<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller {
    public function goodslist(){
      $goods = D('goods');
      $goodsinfo = $goods->select();
      $this->assign('goodsinfo',$goodsinfo);
      $this->display();
    }
    public function goodsdel(){
      $goods = D('Goods');
      $goods_id =I('post.goods_id');
      //dump($goods_id);
      //die;
      $goodsinfo=$goods->find($goods_id);
      if($goodsinfo['goods_isdel']=='下架'){
        $a = array(
          'goods_isdel' => '上架'
        );
        $res =$goods->where(array('goods_id'=>$goods_id))->save($a);
        if($res){
          echo 1;
        }else{
          echo 'error';
        }
      }else{
        $a = array(
          'goods_isdel' => '下架'
        );
        $res =$goods->where(array('goods_id'=>$goods_id))->save($a);
        if($res){
          echo 2;
        }else{
          echo 'error';
        }
      }
    }
    public function goodsupd(){
      $config = C('IMGCONF');
      $goods = D('Goods');
      if(IS_POST){
        $goodsinfo = I('post.');
        $goodsinfo = array(
          'goods_id'=>$goodsinfo['id'],
          'goods_name' => $goodsinfo['name'],
          'goods_price'=>$goodsinfo['price'],
          'goods_vip_price'=>$goodsinfo['vip_price'],
          'goods_num' =>$goodsinfo['num'],
          'goods_weight'=>$goodsinfo['weight'],
          'goods_introduce'=>$goodsinfo['introduce'],
          'goods_isdel' =>$goodsinfo['isdel'],
          'goods_key' =>$goodsinfo['key'],
          'goods_updtime'=>time()
        );
        $goods = D('Goods');
        $up = $goods->imgUpload($config,'pic');
        if($up){
          $goodsinfo = array_merge($up,$goodsinfo);
          $res = $goods->save($goodsinfo);
          if($res){
            $this->success('修改成功',U('goods/goodslist'));
          }else{
            $this->erreo('修改失败');
          }
          //dump($goodsinfo);
        }else{
          $res = $goods->save($goodsinfo);
          if($res){
            $this->success('修改成功',U('goods/goodslist'));
          }else{
            $this->erreo('修改失败');
          }
        }
      }else{
        $goods_id = I('get.goods_id');
        $goodsinfo = $goods->find($goods_id);
        //dump($goodsinfo);
        $this->assign('goodsinfo',$goodsinfo);
        $this->display();
      }
    }
    public function goodsadd(){
      $cate = D('Cate');
      $config = C('IMGCONF');
      if(IS_POST){
            $goodsinfo = I('post.');
            $goodsinfo = array(
              'goods_name' => $goodsinfo['name'],
              'goods_price'=>$goodsinfo['price'],
              'goods_vip_price'=>$goodsinfo['vip_price'],
              'goods_num' =>$goodsinfo['num'],
              'goods_weight'=>$goodsinfo['weight'],
              'goods_introduce'=>$goodsinfo['introduce'],
              'goods_isdel' =>$goodsinfo['isdel'],
              'goods_key' =>$goodsinfo['key'],
              'goods_addtime'=>time(),
              'goods_updtime'=>time()
            );
            $goods = D('Goods');
            $up = $goods->imgUpload($config,'pic');
            if($up){
              $goodsinfo = array_merge($up,$goodsinfo);
              $res = $goods->add($goodsinfo);
              if($res){
                $this->success('添加成功',U('goods/goodslist'));
              }else{
                $this->erreo('添加失败');
              }
              //dump($goodsinfo);
            }else{
              $this->error('上传失败');
            }
      }else{
          $cateinfo = $cate->field("cate_id,cate_name")->where("level = 1")->select();
          $this->assign('cateinfo',$cateinfo);
          $this->display();
      }
    }
    public function goodsrealdel(){
      $goods_id = I('post.goods_id');
      $goods = D('Goods');
      $res = $goods->delete($goods_id);
      if($res){
        echo 1;
      }else{
        echo 'error';
      }
    }
    public function goodsphoto(){
      if(IS_POST){
        $goods_id = I('post.goods_id');
        $goods = D('Goods');
        $pic = D('Pic');
        $config = C('IMGCONF');
        $arr = $goods->imgAllUpload($config,'goods_pic',$goods_id);
        $res = $pic->addAll($arr);
        if($res){
          $this->success('上传成功');
        }else{
          $this->error('上传成功');
        }
      }else{
        $goods_id = I('get.goods_id');
        // dump($goods_id);
        // die;
        $goods=D('Goods');
        $pic=D('Pic');
        $goodsinfo = $goods->field('goods_name,goods_id')->find($goods_id);
        $pic_info = $pic->where('goods_id ='.$goods_id)->select();
        $this->assign('pic_info',$pic_info);
        $this->assign('goods_name',$goodsinfo['goods_name']);
        $this->assign('goods_id',$goodsinfo['goods_id']);
        $this->display();
      }
    }
    public function picDel(){
      $pic = D('Pic');
      $pic_id = I('post.pic_id');
      $picinfo = $pic->where(array('pic_id'=>$pic_id))->find();
      //dump($pic);
      //die;
      // echo $pic_id;
      $res =$pic->delete($pic_id);
      if($res){
        unlink($picinfo['pic_orin']);
        unlink($picinfo['pic_big']);
        unlink($picinfo['pic_mid']);
        unlink($picinfo['pic_sma']);
        echo 1;
      }else{
        echo 'error';
      }

    }
}
